﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DL
{
    public class CarService : Service
    {

        /// <summary>
        /// Gets cars by user and car identifier
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="CarID"></param>
        /// <returns></returns>
        /// 
        public List<Car> GetCarList(int UserID, int? CarID, bool trashed)
        {
            return context.Cars.Include("Model").Where(c => c.UserID == UserID && (c.CarID == CarID || !CarID.HasValue) && c.IsTrashed == trashed).OrderByDescending(x => x.CreateDate).ToList();

        }

        public void SaveCar(Car car, Owner owner, List<string> imagePaths)
        {
            context.Cars.Add(car);
            context.SaveChanges();
            Image image;
            for (int i = 0; i < imagePaths.Count; i++)
            {
                image = new Image() { Path = imagePaths[i], CarID = car.CarID, CreateDate = DateTime.Now };
                context.Images.Add(image);
            }

            owner.CarID = car.CarID;
            owner.CreateDate = DateTime.Now;
            context.Owners.Add(owner);

            context.SaveChanges();
        }

        #region Get Id By Name

        public int GetModelByName(string modelName)
        {
            var model = context.Models.Where(x => x.Name == modelName).FirstOrDefault();
            if (model != null)
            {
                return model.ModelID;
            }
            return -1;
        }

        public int GetFuelTypeByName(string fuelTypeName)
        {
            var fuelType = context.FuelTypes.Where(x => x.Name == fuelTypeName).FirstOrDefault();
            if (fuelType != null)
            {
                return fuelType.FuelTypeID;
            }
            return -1;
        }

        public int GetCategoryByName(string categoryName)
        {
            var category = context.Categories.Where(x => x.Name == categoryName).FirstOrDefault();
            if (category != null)
            {
                return category.CategoryID;
            }
            return -1;
        }

        public int GetColorByName(string colorName)
        {
            var collor = context.Colors.Where(x => x.Name == colorName).FirstOrDefault();
            if (collor != null)
            {
                return collor.ColorID;
            }
            return -1;
        }
        #endregion

        public string GetImage(int carID)
        {
            Image image = context.Images.FirstOrDefault(x => x.CarID == carID);
            if (image != null)
            {
                return image.Path;
            }
            return string.Empty;
        }

        public void ChangeFav(int carID)
        {
            var car = context.Cars.FirstOrDefault(x => x.CarID == carID);
            if (car.IsFav)
            {
                car.IsFav = false;
            }
            else
                car.IsFav = true;
            context.SaveChanges();
        }

        public void AddTrashed(int carID)
        {
            var car = context.Cars.FirstOrDefault(x => x.CarID == carID);
            if (car.IsTrashed)
            {
                car.IsTrashed = false;
            }
            else
                car.IsTrashed = true;
            context.SaveChanges();
        }

        public List<Category> GetCategories()
        {
            return context.Categories.OrderByDescending(x=>x.SortIndex).ToList();
        }
        public List<FuelType> GetFuelTypes()
        {
            return context.FuelTypes.OrderByDescending(x => x.SortIndex).ToList();
        }
        public List<Color> GetColors()
        {
            return context.Colors.OrderBy(x => x.Name).ToList();
        }
        public List<Manufacturer> GetManufacturers()
        {
            return context.Manufacturers.OrderBy(x => x.Name).ToList();
        }
        
        public List<Model> GEtModelByManufacturer(int manufacturerID)
        {
            return context.Models.Where(x => x.ManufacturerID == manufacturerID).ToList();
        }
    }
}
